﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using SistemaAcademicoBO.Entities;
using SistemaAcademicoBO;

namespace SistemaAcademicoDAL
{
    public class SubjectDAO : AbstratcDAO, IDAO<IEntity>
    {
        public SubjectDAO()
        {
            connection = new Connection();
        }

        public IEntity GetRecord<K>(K id)
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();

            ProcedureCall procedureCall = new ProcedureCall("pr_retrieveSubject", connection.GetSQLConnection());
            procedureCall.Parameters.Add(new ProcedureParam("@SubjectId", SqlDbType.Int, 0, id));

            procedureCall.Execute(true);

            List<IEntity> resultSet = procedureCall.ExtractFromResultSet(typeof(Subject));

            connection.CloseConnection();

            if (resultSet.Count != 1)
                return null;

            return resultSet[0];
        }

        public List<IEntity> GetAllRecords()
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();

            ProcedureCall procedureCall = new ProcedureCall("pr_retrieveAllSubjects", connection.GetSQLConnection());

            procedureCall.Execute(true);

            List<IEntity> resultSet = procedureCall.ExtractFromResultSet(typeof(Subject));

            connection.CloseConnection();

            return resultSet;
        }

        public void SetRecord(IEntity entity)
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();
            ProcedureCall procedureCall = new ProcedureCall("pr_storeSubject", connection.GetSQLConnection());

            Subject subject = (Subject)entity;

            procedureCall.Parameters.Add(new ProcedureParam("@SubjectId", SqlDbType.Int, 0, subject.SubjectId));
            procedureCall.Parameters.Add(new ProcedureParam("@CargaHoraria", SqlDbType.Int, 0, subject.CargaHoraria));
            procedureCall.Parameters.Add(new ProcedureParam("@SubjectName", SqlDbType.VarChar, 100, subject.SubjectName));

            procedureCall.Execute(false);

            connection.CloseConnection();
        }

        public void RemoveRecord<K>(K id)
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();

            ProcedureCall procedureCall = new ProcedureCall("pr_removeSubject", connection.GetSQLConnection());

            procedureCall.Parameters.Add(new ProcedureParam("@SubjectId", SqlDbType.Int, 0, id));

            procedureCall.Execute(false);

            connection.CloseConnection();
        }


    }
}
